package tools.old.files;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import dao.account.DataLayerAccount;
import dao.server.DataLoadIP;
import account.model.SubTeam;
import prototyping.client.AbstractMessage;
import prototyping.client.student.model.StudentClient;
import prototyping.client.teacher.model.TeacherClient;

public class TeacherFrame extends JFrame implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userName;

	private JScrollPane srpHelp;
	private JTextArea txaHelp;
	private JLabel lblServing;

	public TeacherFrame(String userName) {
		super(userName);
//		this.userName = SubTeam.getInstance().getAcc().getVoornaam();
		initComponents();
		
		DataLoadIP ip = new DataLoadIP();
		ip.openConn();
		
		TeacherClient tc = new TeacherClient(ip.LoadServerIP(), userName);
		tc.addObserver(this);
		tc.start();
	}

	private void initComponents() {

		srpHelp = new JScrollPane();
		txaHelp = new JTextArea();
		txaHelp.setEditable(false);
		lblServing = new JLabel();
		
//		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		txaHelp.setColumns(20);
		txaHelp.setRows(5);
		srpHelp.setViewportView(txaHelp);

		lblServing.setText("Now serving...");

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														lblServing,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														srpHelp,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														380, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(lblServing)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(srpHelp,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										160,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		
	
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setTitle("Hallo " + userName);
		this.setVisible(true);
		this.setResizable(false);
		this.pack();
		this.setSize(560, 321);

		Dimension center = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(center.width / 2 - this.getSize().width / 2,
				center.height / 2 - this.getSize().height / 2);
	}

	@Override
	public void update(Observable o, Object arg) {
		AbstractMessage amsg = (AbstractMessage) arg;
		txaHelp.append(amsg.getUser() + ": " + amsg.getMessage() + "\n");
		txaHelp.setCaretPosition(txaHelp.getText().length() - 1);
	}

}
